from django.core.signing import JSONSerializer
from rest_framework.response import Response


class MyResult():
    def __init__(self,code:int=200,msg:str='success',result=None):
        self.code = code
        self.msg = msg
        self.result = result

    def to_dict(self):
        return {
            "code":self.code,
            "msg":self.msg,
            "result":self.result
        }
    def __repr__(self):
        return self.to_dict().__repr__()

class Result(dict):
    def __init__(self, code=200, msg='success', data=None):
        super().__init__()
        self['code'] = code
        self['msg'] = msg
        self['data'] = data

class MyPage:
    def __init__(self,page:int=1,page_size:int=10,total:int=0,page_rows:list=None):
        self.page = page
        self.page_size = page_size
        self.total = total
        self.page_rows = page_rows
        self.total_pages =0
        #计算总页数
        if self.total % self.page_size == 0:
            self.total_pages = self.total // self.page_size
        else:
            self.total_pages = self.total // self.page_size + 1


    def to_dict(self):
        return {
            "page":self.page,
            "page_size":self.page_size,
            "total":self.total,
            "total_pages":self.total_pages,
            "page_rows":self.page_rows
        }


if __name__ == "__main__":
    result = MyResult(code=400,msg='error',result=MyPage(page=1,page_size=10,total=101,data=[{'id':1,'name':'张三'},{'id':2,'name':'李四'}]).to_dict())
    print(result)